package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.ImageModel;
import com.hangzhou.xyj.ui.widget.views.MyImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/22.
 */
public class PublishImgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<ImageModel> imageList = new ArrayList<>();
    private int itemHeight;
    private OnItemClickListener mListener;
    private OnMaxItemDeleteListener mOnMaxItemDeleteListener;

    public PublishImgAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public int getItemHeight() {
        return itemHeight;
    }

    /**
     * 添加一个图片数据到List集合的最后
     *
     * @param model
     */
    public void addData(ImageModel model) {
        imageList.add(model);
    }

    /**
     * 返回图片的url
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/5/7 17:43
     */
    public List<String> getImageList() {
        List<String> resList = new ArrayList<>();
        for (ImageModel imageModel : imageList) {
            resList.add(imageModel.getPath());
        }
        return resList;
    }

    @Override
    public int getItemCount() {
        if (imageList.size() == 0) {
            return 1;
        } else {
            return imageList.size() + 1;

        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public void setOnMaxItemDeleteListener(OnMaxItemDeleteListener listener) {
        this.mOnMaxItemDeleteListener = listener;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        final MyViewHolder holder = (MyViewHolder) viewHolder;
        if (position == imageList.size()) {//如果是最后一个
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onLastItemClick(imageList.size());
                }
            });
        }
        if (position < getItemCount() - 1) {//不是最后一个,position从0开始
            holder.image.setVisibility(View.VISIBLE);
            holder.imageDefault.setVisibility(View.GONE);
            holder.imgDelete.setVisibility(View.VISIBLE);
            holder.imgDelete.setAlpha(0.9f);
            holder.image.displayImage("file:///" + imageList.get(position).getPath());
            holder.imgDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final int pos = holder.getAdapterPosition();
                    imageList.remove(pos);
                    notifyItemRemoved(pos);
                    if (imageList.size() == 8) {
                        mOnMaxItemDeleteListener.OnMaxItemDelete(imageList.size() + 1);
                    }
                }
            });
        } else {//最后一个
            holder.image.setVisibility(View.GONE);
            holder.imageDefault.setVisibility(View.VISIBLE);
            holder.imgDelete.setVisibility(View.GONE);
            if (position == 9) {
                holder.itemView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.layout_publish_image_item, viewGroup, false);
        return new MyViewHolder(view);
    }

    public interface OnItemClickListener {
        void onLastItemClick(int currentSize);
    }

    public interface OnMaxItemDeleteListener {
        void OnMaxItemDelete(int maxSize);
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        MyImageView image;
        MyImageView imageDefault;
        MyImageView imgDelete;//删除按钮

        public MyViewHolder(final View itemView) {
            super(itemView);
            image = (MyImageView) itemView.findViewById(R.id.miv_image);
            imageDefault = (MyImageView) itemView.findViewById(R.id.miv_default);
            imgDelete = (MyImageView) itemView.findViewById(R.id.miv_delete);
            itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    itemHeight = itemView.getMeasuredHeight();
                    return true;
                }
            });
        }
    }
}
